package com.kkarma.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: karma
 * @Date: 2022/3/30 0030 - 03 - 30 - 9:06
 * @Description: com.kkarma.util.redis
 * @version: 1.0
 */
@Component
public class RedisCache {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * method description:缓存基本数据对象，Integer、String、实体类等
     * @param key 缓存key
     * @param value 缓存值value
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:10
     */
    public <T> void setCacheObject(final String key, final T value)
    {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * method description:缓存基本数据对象，Integer、String、实体类等
     * @param key 缓存key
     * @param value 缓存值value
     * @param timeout 有效时间
     * @param timeUnit 时间单位
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:15
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }


    /**
     * method description: 设置redis中指定缓存key的过期时间
     * @param key 缓存key
     * @param timeout 有效时间
     * @return: true代表设置成功，false代表设置失败
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:31
     */
    public boolean expire(final String key, final long timeout)
    {
        return expire(key, timeout, TimeUnit.SECONDS);
    }


    /**
     * method description:缓设置redis中指定缓存key的过期时间的重载
     * @param key 缓存key
     * @param timeout 有效时间
     * @param unit 时间单位
     * @return: true代表设置成功，false代表设置失败
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:31
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit)
    {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * method description:缓设置redis中指定缓存key的过期时间的重载
     * @param key 缓存key
     * @return: 指定key对应的对象数据
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:40
     */
    public <T> T getCacheObject(final String key)
    {
        ValueOperations<String, T> valueOperation = redisTemplate.opsForValue();
        return valueOperation.get(key);
    }

    /**
     * method description:删除单个对象
     * @param key 缓存key
     * @return: true代表删除成功, false代表删除失败
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 9:50
     */
    public boolean deleteObject(final String key)
    {
        return redisTemplate.delete(key);
    }

    /**
     * method description:删除集合对象
     * @param collection 多个对象
     * @return: long
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:02
     */
    public long deleteObject(final Collection collection)
    {
        return redisTemplate.delete(collection);
    }

    /**
     * method description: 缓存List数据
     * @param key 缓存key值
     * @param dataList 待缓存的List数据
     * @return: long
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:13
     */
    public <T> long setCacheList(final String key, final List<T> dataList)
    {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }

    /**
     * method description: 获得缓存的list对象
     * @param key 缓存key值
     * @return: List 缓存key值对应的数据
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:13
     */
    public <T> List<T> getCacheList(final String key)
    {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * method description: 缓存Set类型的数据
     * @param key 缓存key值
     * @return: 缓存数据的对象
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:19
     */
    public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
    {
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext())
        {
            setOperation.add(it.next());
        }
        return setOperation;
    }

    /**
     * method description: 获得缓存的set
     * @param key 缓存key值
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:19
     */
    public <T> Set<T> getCacheSet(final String key)
    {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * method description: 缓存Map类型的数据
     * @param key 缓存key值
     * @param dataMap
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:23
     */
    public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
    {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    /**
     * method description: 获得缓存的Map类型的数据
     * @param key 缓存key值
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:23
     */
    public <T> Map<String, T> getCacheMap(final String key)
    {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * method description: 保存hash类型的数据
     * @param key key值
     * @param hKey Hash key
     * @param value Hash value
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:30
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value)
    {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    /**
     * method description: 获取Hash中的数据
     * @param key key值
     * @param hKey Hash key
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:30
     */
    public <T> T getCacheMapValue(final String key, final String hKey)
    {
        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }

    /**
     * method description: 删除Hash中的数据
     * @param key key值
     * @param hKey Hash key
     * @return:
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:33
     */
    public void delCacheMapValue(final String key, final String hKey)
    {
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.delete(key, hKey);
    }

    /**
     * method description: 获取多个Hash中的数据
     * @param key key值
     * @param hKeys Hash key集合
     * @return: Hash对象集合
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:33
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
    {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    /**
     * method description: 获得缓存的基本对象列表
     * @param pattern 字符串前缀
     * @return: 对象列表
     * @author: kkarma
     * @date: 2022/3/30 0030 - 03 - 30 - 10:33
     */
    public Collection<String> keys(final String pattern)
    {
        return redisTemplate.keys(pattern);
    }
}
